/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.carbswang.ohos.numberpickerview.library;

import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.lang.reflect.Field;

/**
 * xml 属性读取工具类
 *
 * @since 2021-04-07
 */
public class AttrValue {
    private AttrValue() {
        // do nothing
    }

    /**
     * 读取 xml 属性值
     *
     * @param attrSet 属性集合
     * @param key xml 属性键
     * @param defValue 默认值
     * @param <T> 属性值类型
     * @return 属性值
     */
    @SuppressWarnings("unchecked cast")
    public static <T> T getPrimitive(AttrSet attrSet, String key, T defValue) {
        if (!attrSet.getAttr(key).isPresent()) {
            return (T) defValue;
        }

        Attr attr = attrSet.getAttr(key).get();
        if (defValue instanceof String) {
            return (T) attr.getStringValue();
        } else if (defValue instanceof Long) {
            return (T) (Long) (attr.getLongValue());
        } else if (defValue instanceof Float) {
            return (T) (Float) (attr.getFloatValue());
        } else if (defValue instanceof Integer) {
            return (T) (Integer) (attr.getIntegerValue());
        } else if (defValue instanceof Boolean) {
            return (T) (Boolean) (attr.getBoolValue());
        } else if (defValue instanceof Color) {
            return (T) (attr.getColorValue());
        } else {
            return (T) defValue;
        }
    }

    /**
     * 读取属性的strarray值
     *
     * @param context context
     * @param attrSet attrSet
     * @param key key
     * @return String[]
     */
    public static String[] getStringArray(Context context, AttrSet attrSet, String key) {
        String name = AttrValue.getPrimitive(attrSet, key, "");
        if (name.startsWith("$strarray:")) {
            try {
                String name2 = name.split(":")[1];
                Field field = ResourceTable.class.getField("Strarray_" + name2);
                if (field != null) {
                    int id = field.getInt("Strarray_" + name2);
                    return context.getResourceManager().getElement(id).getStringArray();
                }
            } catch (NoSuchFieldException | IllegalAccessException
                | NotExistException | WrongTypeException | IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 读取属性的Element值
     *
     * @param attrSet attrSet
     * @param key key
     * @return Element
     */
    public static Element getElement(AttrSet attrSet, String key) {
        Element element = null;
        if (attrSet.getAttr(key).isPresent()) {
            element = attrSet.getAttr(key).get().getElement();
        }
        return element;
    }

    /**
     * 读取 xml 尺寸型属性, 例如 cornerRadius="4vp"
     *
     * @param attrSet 属性集合
     * @param key 属性键
     * @param defDimensionValue 默认值
     * @return 属性值
     */
    public static int getDimension(AttrSet attrSet, String key, int defDimensionValue) {
        if (!attrSet.getAttr(key).isPresent()) {
            return defDimensionValue;
        }

        Attr attr = attrSet.getAttr(key).get();
        return attr.getDimensionValue();
    }
}